﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RuEn
{
    /// <summary>
    /// Attribute class for Collection Duplicate Rules. Check for duplication in an collection
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
    public class IntCollectionRule : CollectionRuleBase
    {
        private string _errorMessage = "Duplicate Items found";

        /// <summary>
        /// Default Constructoe
        /// </summary>
        public IntCollectionRule()
        {
        }

        /// <summary>
        /// Overloaded Constructor
        /// </summary>
        /// <param name="ErrorMessage">Custom Error message from the User</param>
        public IntCollectionRule(string ErrorMessage)
        {
            _errorMessage = ErrorMessage;
        }

        /// <summary>
        /// Public property to assign custom error message
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }

        public bool Validate(IEnumerable<int> value, string ErrorMessage)
        {
            bool state = true;
            List<int> tempCol = new List<int>();
            if (value != null)
            {
                foreach (int s in value)
                {
                    if (tempCol.Contains(s))
                    {
                        state = false;
                        break;
                    }
                    else
                    {
                        tempCol.Add(s);
                    }
                }
            }
            return state;
        }
    }
}
